var ratPack = $.sammy(function() {
	this.element_selector = '#DashboardController';
	
	this.get('#/started', function(context){
		if(application.actions.checkProject()){
			console.log('Have project');
			window.location.href= "#/main";
		}else{
			console.log('The project not found');
			
		}
	});
	
	this.get('', function(context){
		
		if(application.actions.checkProject()){
			console.log('Have project');
			window.location.href= "#/main";
		}else{
			console.log('The project not found');
		}
	});	
	this.get('#/main', function(context){
		if(application.actions.checkProject()){
			console.log('Have project');
			application.teamplate.getBoadHtml(context);
		}else{
			console.log('The project not found');
			window.location.href= "#/started";
		}
	});
	

	
});

function loadLadda(){
	try{
		user.ladda = Ladda.bind('button');
	}catch(e){
		console.log(e.message);
	}
}
$(function() {
	switch(window.location.hash){
		case "#/started":
			ratPack.run('#/started');
		break;
		case "#/main":
			
			if(application.actions.checkProject()){
				console.log('Have project');
				application.teamplate.getBoadHtml(false);
			}else{
				console.log('The project not found');
				window.location.href= "#/started";
			}
		break;
		default:
			if(application.actions.checkProject()){
				console.log('Have project');
				application.teamplate.getBoadHtml(false);
			}else{
				console.log('The project not found');
			}
		break;
	}
	
	
	
	
	
	
	$(".get-started").live('click',function(e){
		if(window.location.hash == "#/started"){
			ratPack.run('#/started');
			
			application.actions.started(this);
			
		}
	});
	
	
	$(".pool_dashboard").css({height:$(window).height()});
});


var application = {
	ladda:null,
	actions:{
		checkProject:function(){
			
			if(project.length!=0) return true;
			else return false;
		},
		started:function(_this){
			
			var loading = Ladda.create(_this);
			loading.start();
			$.post("/projects/create",{data:Math.random() * 10000},function(res){
				if(res.length != 0){
					if(typeof res.code != "undefined"){
						if(res.code==400){
							console.log(res.message);
							
						}else{
							project = res.data;
							
							window.location.href = "#/main";
						}
					}else{
						console.log('Result not format');
					}
				}else{
					console.log('Result not format');
				}
				loading.stop();
			}).fail(function(xhr) {
				console.log(xhr);
				loading.stop();
			});
		}
	
	},
	teamplate:{
		getBoadHtml: function(context){
			$( "#home" ).css({opacity:0});

			$( "#home" ).fadeOut( 2000, function() {
				$(".pool_dashboard").show( 'fast',function(){
				
				});
			});
			
			
			// console.log(context.app)
			// $.get(homeURL+"/users/login",function(html){
				// context.app.swap('123');
				// context.$element().html(html);
			// });
		
		}
		
	}
};

